﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

using Inbox.Domain;
using System.Collections;

namespace Inbox.Bussiness
{
    [DataContract]
    public class PlanningContract
    {
        [DataMember]
        public Guid ID { get; set; }
        [DataMember]
        public string ResponsiblePerson { get; set; }
        [DataMember]
        public string Title { get; set; }
        [DataMember]
        public int? Type { get; set; }
        [DataMember]
        public DateTime? StartDate { get; set; }
        [DataMember]
        public DateTime? EndDate { get; set; }

        [DataMember]
        public string Description { get; set; }

    }

    [DataContract]
    public class MachineContract
    {
        [DataMember]
        public Guid ID { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public decimal? PurchaseAmount { get; set; }

        [DataMember]
        public decimal? NetAmount { get; set; }

        [DataMember]
        public DateTime? StartUsedDate { get; set; }

        [DataMember]
        public DateTime? EndUsedDate { get; set; }

        [DataMember]
        public string Description { get; set; }
    }
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "PlanningService" in code, svc and config file together.
    public class PlanningService : IPlanningService
    {

        public IList<PlanningContract> GetListPlanning()
        {
            using (InboxContext context = new InboxContext())
            {
                var query = from q in context.GetTable<Planning>()
                            select new PlanningContract
                            {
                                ID = q.ID,
                                ResponsiblePerson = q.ResposiblePerson,
                                Title = q.Title,
                                Type = q.Type,
                                StartDate = q.StartDate,
                                EndDate = q.EndDate
                            };
                return query.ToList();
            }
        }

        public PlanningContract GetPlanning(Guid planID)
        {
            using (InboxContext context = new InboxContext())
            {
                var query = from q in context.GetTable<Planning>()
                            where q.ID == planID
                            select new PlanningContract
                            {
                                ID = q.ID,
                                ResponsiblePerson = q.ResposiblePerson,
                                Title = q.Title,
                                Type = q.Type,
                                StartDate = q.StartDate,
                                EndDate = q.EndDate,
                                Description = q.Description
                            };
                return query.FirstOrDefault();
            }
        }

        public IList<MachineContract> GetMachineLinked(Guid planID)
        {
            using (InboxContext context = new InboxContext())
            {
                var query = from m in context.GetTable<Machine_Planning>()
                            join p in context.GetTable<Machine>() on m.Machine_ID equals p.ID
                            where m.Planning_ID == planID
                            select new MachineContract
                            {
                                ID = p.ID,
                                Description = m.Description,
                                Name = p.Name,
                                PurchaseAmount = p.PurchaseValue,
                                NetAmount = p.NetValue,
                                StartUsedDate = p.UsedDate,
                                EndUsedDate = p.EndUseDate
                            };

                return query.ToList();
            }
        }

        public IList<MachineContract> GetMachineForNew()
        {
            using (InboxContext context = new InboxContext())
            {
                var query = from m in context.GetTable<Machine>()
                            select new MachineContract
                            {
                                ID = m.ID,
                                Name = m.Name,
                                PurchaseAmount = m.PurchaseValue,
                                NetAmount = m.NetValue,
                                StartUsedDate = m.UsedDate,
                                EndUsedDate = m.EndUseDate
                            };

                return query.ToList();
            }
        }

        public bool SavePlan(string desp, string title, string responsiblePerson, DateTime fromDate, DateTime toDate, int planStatus, int planType, IDictionary<Guid, string> ListMachine)
        {
            try
            {
                using (InboxContext context = new InboxContext())
                {
                    Planning newPlan = new Planning()
                    {
                        ID = Guid.NewGuid(),
                        Title = title,
                        ResposiblePerson = responsiblePerson,
                        Description = desp,
                        Status = planStatus,
                        Type = planType,
                        StartDate = fromDate,
                        EndDate = toDate,
                        CreateDate = DateTime.Now
                    };
                    context.InsertEntity(newPlan);

                    foreach (Guid ID in ListMachine.Keys)
                    {
                        Machine_Planning newMached = new Machine_Planning()
                        {
                            ID = Guid.NewGuid(),
                            Machine_ID = ID,
                            Planning_ID = newPlan.ID,
                            Description = ListMachine[ID],
                            CreateDate = DateTime.Now
                        };
                        context.InsertEntity(newMached);
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool DeletePlan(Guid planID)
        {
            try
            {
                using (InboxContext context = new InboxContext())
                {
                    var deleteMachinePlan = from q in context.GetTable<Machine_Planning>()
                                            where q.Planning_ID == planID
                                            select q;

                    foreach (var machinePlan in deleteMachinePlan.ToList())
                    {
                        context.DeleteEntity(machinePlan);
                    }

                    var deletePlan = from q in context.GetTable<Planning>()
                                     where q.ID == planID
                                     select q;

                    context.DeleteEntity(deletePlan.First());
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
                return false;
            }
        }
    }
}
